using System;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Message.Interop;
using System.Resources;
using System.Reflection;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.Xml;
using System.Threading;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;

using System.ComponentModel.Design;

//using ContextAdderHelper;
namespace GML.BizTalk.ContextAdderPipelineComponent
{

	
	[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
	[ComponentCategory(CategoryTypes.CATID_Any)]
    [System.Runtime.InteropServices.Guid("6999D29C-FCB2-4a96-8EC0-F62C3996BB39")]
	public class AddContextProperties: IBaseComponent,IComponent,IComponentUI,IPersistPropertyBag
	{

	
		public AddContextProperties()
		{
            ContextValueCollection c = new ContextValueCollection();
            c.Dirty+=new IsDirty(DirtyFired);
            Properties = c;

            //List<ContextProperty> cp = new List<ContextProperty>();
            //Properties = cp;
		}
		void DirtyFired()
		{
			Properties = _Properties;
		}
        public ContextValueCollection Properties
        //public List<ContextProperty> Properties
        {
			get{return _Properties;}

			set{_Properties = value;}
			
		}
        private ContextValueCollection _Properties;
        //private List<ContextProperty> _Properties;

		#region IBaseComponent Members

		public string Description
		{
			get
			{
				// TODO:  Add MyPipelineClass.Description getter implementation
				return "ContextProperty Adder Class";
			}
		}

		public string Name
		{
			get
			{
				// TODO:  Add MyPipelineClass.Name getter implementation
                return "ContextProperty Adder";
			}
		}

		public string Version
		{
			get
			{
				// TODO:  Add MyPipelineClass.Version getter implementation
				return "1.0.0.0";
			}
		}

		#endregion

		#region IComponent Members

		public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
		{
			
			IEnumerator e = Properties.GetEnumerator();
			while(e.MoveNext())
			{
                //ContextProperty v = (ContextProperty)e.Current;
                ContextValue v = (ContextValue)e.Current;
                if (!v.Promote)
                //if (!v.PromteProperty)
                    pInMsg.Context.Write(v.Key, v.Namespace, v.Value);
				else
					pInMsg.Context.Promote(v.Key ,v.Namespace,v.Value);

			}
			return pInMsg;
		}

		#endregion
	
		
		#region IComponentUI Members

		public System.Collections.IEnumerator Validate(object projectSystem)
		{
			// TODO:  Add MyPipelineClass.Validate implementation
			return null;
		}

		public System.IntPtr Icon
		{
			get
			{
				    ResourceManager rm = new ResourceManager("GML.BizTalk.ContextAdderPipelineComponent.Resources",Assembly.GetExecutingAssembly());
				Bitmap bm = (Bitmap)rm.GetObject("MyIcon");
				return bm.GetHicon();
			}
		}

		#endregion
	
		#region IPersistPropertyBag Members

		public void InitNew()
		{
		}

		public void GetClassID(out Guid classID)
		{
            classID = new Guid("{98324511-3C7B-4afa-A3B9-25DBA930C519}");
		}

		object ReadProperty(IPropertyBag propertyBag,string key)
		{
			object val =null;
			try
			{
				propertyBag.Read(key,	out val,0);
			}
			catch
			{
			}
			return val;

		}
		public void Load(IPropertyBag propertyBag, int errorLog)
		{

            try
            {
                ContextAdder oContextAdder = null;
                object val = null;
                //val = ReadProperty(propertyBag, "PropertiesCount");
                val = ReadProperty(propertyBag, "ConfigurationXml");
                if (val != null)
                {
                    string sxml = Convert.ToString(val);
                    System.Diagnostics.Debug.WriteLine("ConfigurationXml = " + sxml);
                    oContextAdder = ContextAdder.GetDeserializedObject(sxml);


                    ContextValue v = null;
                    foreach (ContextProperty cp in oContextAdder.ContextProperties)
                    {
                        v = new ContextValue();
                        v.Key = cp.Key;
                        v.Namespace = cp.Namespace;
                        v.Promote = cp.PromteProperty;
                        v.Value = cp.Value;
                        ((IList)Properties).Add(v);
                    }
                    //ContextValue v = null;
                    //for (int i = 0; i < count; i++)
                    //{
                    //    //v = new ContextValue();
                    //    v = new ContextProperty();


                    //    val = ReadProperty(propertyBag,String.Format("PropertiesKey{0}",i.ToString()));
                    //    v.Key = val.ToString();
                    //    val = ReadProperty(propertyBag,String.Format("PropertiesNS{0}",i.ToString()));
                    //    v.Namespace = val.ToString();
                    //    val = ReadProperty(propertyBag,String.Format("PropertiesValue{0}",i.ToString()));
                    //    v.Value = val.ToString();
                    //    val = ReadProperty(propertyBag,String.Format("PropertiesPromote{0}",i.ToString()));
                    //    v.Promote = Boolean.Parse(val.ToString());

                    //    ((IList)Properties).Add(v);

                    //}
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("ContextAdder.cs:: " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }
		

		public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
		{
            try
            {
                //ContextProperty v = null;
                ContextValue v = null;
                if (Properties != null)
                {
                    List<ContextProperty> cp = new List<ContextProperty>();
                    ContextProperty cpx = null;
                    ContextAdder oContextAdder = new ContextAdder();
                    foreach (ContextValue cv in Properties)
                    {
                        cpx = new ContextProperty();
                        cpx.Key = cv.Key;
                        cpx.Namespace = cv.Namespace;
                        cpx.PromteProperty = cv.Promote;
                        cpx.Value = cv.Value;
                        cp.Add(cpx);
                    }
                    oContextAdder.ContextProperties = cp.ToArray();
                    string sConfigXml = oContextAdder.GetSerializedXml();
                    object o = (object)sConfigXml;
                    propertyBag.Write("ConfigurationXml", ref o);

                    //object o = Properties.Count;
                    //propertyBag.Write("PropertiesCount",ref o);

                    //for(int i=0;i<Properties.Count;i++)
                    //{

                    //    v = Properties[i];
                    //    o = (object)v.Key;
                    //    propertyBag.Write(String.Format("PropertiesKey{0}",i.ToString()),ref o);
                    //    o = (object)v.Namespace;
                    //    propertyBag.Write(String.Format("PropertiesNS{0}",i.ToString()),ref o);
                    //    o = (object)v.Value;
                    //    propertyBag.Write(String.Format("PropertiesValue{0}",i.ToString()),ref o);
                    //    o = (object)v.Promote;
                    //    propertyBag.Write(String.Format("PropertiesPromote{0}",i.ToString()),ref o);




                    //}
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("ContextAdder.cs:: " + ex.Message + Environment.NewLine + ex.StackTrace);
            }		
		}

		#endregion
	}
	
}
